import sys

sys.path.append('../')

from config import *


@app.route('/sysconfig.html', methods = ['GET'])
def config():
    if not 'username' in session:
        return redirect(url_for('login'))

    user = {
        'username': session['username'],
        'uid': session['uid'],
        'role': session['role']
    }

    return render_template('sysconfig.html', user = user)


@app.route('/detail.html', methods = ['GET'])
def app23():
    if not 'username' in session:
        return redirect(url_for('login'))

    return render_template('detail.html')


@app.route('/intention.html', methods = ['GET'])
def intention():
    if not 'username' in session:
        return redirect(url_for('login'))

    user = {
        'username': session['username'],
        'uid': session['uid'],
        'role': session['role']
    }

    return render_template('intention.html', user = user)


@app.route('/contact.html', methods = ['GET'])
def contact():
    if not 'username' in session:
        return redirect(url_for('login'))

    user = qushu_cv_match_user_coll.find_one({'uid': session['uid']})

    contact_list = []

    for id in user['contact_list']:
        item = qushu_resume_coll.find_one({'_id': ObjectId(id)}, {'token': 0})

        if not item:
            continue

        if qushu_contact_coll.find_one({'id': id}):
            item['contact_information'] = qushu_contact_coll.find_one({'id': id})['contact_information']

        item['id'] = str(item['_id'])

        del item['_id']

        contact_list.append(item)

    print(user)
    return render_template('contact.html', user = user, left_contact_list = contact_list[::2], right_contact_list = contact_list[1::2])


@app.route('/admin.html', methods = ['GET'])
def admin():
    if not 'username' in session:
        return redirect(url_for('login'))

    user = {
        'username': session['username'],
        'uid': session['uid'],
        'role': session['role']
    }

    contact_list = []

    for i in qushu_contact_coll.find():
        item = qushu_resume_coll.find_one({'_id': ObjectId(i['id'])}, {'_id': 0, 'token': 0})
        item['contact_information'] = i['contact_information']
        item['id'] = i['id']

        contact_list.append(item)

    user_list = []

    for i in qushu_cv_match_user_coll.find({}, {'_id': 0}):
        user_list.append(i)

    return render_template('admin.html', user = user, contact_list = contact_list, user_list = user_list)


@app.route('/main.html', methods = ['GET'])
def app22():
    if not 'username' in session:
        return redirect(url_for('login'))

    user = {
        'username': session['username'],
        'uid': session['uid'],
        'role': session['role']
    }

    return render_template('main.html', user = user)


@app.route('/login.html', methods = ['GET', 'POST'])
def login():
    return render_template('login.html', errinfo = '')
